package online.foxnull2.foxmall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import online.foxnull2.foxmall.product.entity.BrandEntity;
import online.foxnull2.foxmall.product.entity.CategoryBrandRelationEntity;
import online.foxnull2.foxmall.product.mapper.BrandMapper;
import online.foxnull2.foxmall.product.mapper.CategoryBrandRelationMapper;
import online.foxnull2.foxmall.product.mapper.CategoryMapper;
import online.foxnull2.foxmall.product.service.CategoryBrandRelationService;
import online.foxnull2.foxmall.product.utils.PageUtils;
import online.foxnull2.foxmall.product.utils.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl
        extends ServiceImpl<CategoryBrandRelationMapper, CategoryBrandRelationEntity>
        implements CategoryBrandRelationService {

    private final BrandMapper brandMapper;
    private final CategoryMapper categoryMapper;
    private BrandServiceImpl brandService;

    @Autowired
    public CategoryBrandRelationServiceImpl(BrandMapper brandMapper,
                                            CategoryMapper categoryMapper) {
        this.brandMapper = brandMapper;
        this.categoryMapper = categoryMapper;
    }

    @Autowired
    public void setBrandService(BrandServiceImpl brandService) {
        this.brandService = brandService;
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveCategoryBrandRelation(CategoryBrandRelationEntity categoryBrandRelation) {
        Long brandId = categoryBrandRelation.getBrandId();
        Long catelogId = categoryBrandRelation.getCatelogId();
        categoryBrandRelation.setBrandName(brandMapper.selectById(brandId).getName());
        categoryBrandRelation.setCatelogName(categoryMapper.selectById(catelogId).getName());

        this.save(categoryBrandRelation);
    }

    @Override
    public void updateBrand(Long brandId, String name) {
        CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
        categoryBrandRelationEntity
                .setBrandId(brandId)
                .setBrandName(name);

        this.update(categoryBrandRelationEntity, new UpdateWrapper<CategoryBrandRelationEntity>().eq("brand_id", brandId));
    }

    @Override
    public void updateCategory(Long catId, String name) {
        CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
        categoryBrandRelationEntity
                .setCatelogId(catId)
                .setCatelogName(name);

        this.update(categoryBrandRelationEntity, new UpdateWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));
    }

    @Override
    public List<BrandEntity> listBrandsByCatId(Long catId) {
        List<CategoryBrandRelationEntity> categoryBrandRelationEntityList = baseMapper.selectList(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));
        return categoryBrandRelationEntityList.stream().map(categoryBrandRelationEntity -> brandService.getById(categoryBrandRelationEntity.getBrandId()))
                .collect(Collectors.toList());
    }

}